package demo;

import java.util.Arrays;

public class MyArrayList {

    public int[] array;
    public int usedSize;
    public static final int DEFAULT_SIZE = 10;

    public MyArrayList() {
        this.array = new int[DEFAULT_SIZE];

    }



    public int indexOf(int toFind) {
        for (int i = 0; i < this.usedSize; i++) {
            if(this.array[i] == toFind) {
                return i;
            }
        }
        return -1;
    }


    public void add(int data) {
        if(isFull()) {
            //扩容
        }
        this.array[this.usedSize] = data;
        this.usedSize = this.usedSize + 1;
    }

    /**
     *
     * 检查add数据的时候，pos是否合法
     * @param pos
     */
    private void checkIndex(int pos) {
        if(pos < 0 || pos >this.usedSize) {
            throw new AddIndexOutOfException("add位置不合法");
        }

    }

    public void add(int pos,int data) {
        //判断位置是否合法
        if(isFull() == true) {
            //扩容
        }
        //[1 2  3 4] pos的位置不能为5，因为顺序表连续存放，不能跳着插
        if(pos < 0 || pos >= this.array.length) {

        }

        for (int i = this.usedSize - 1; i >= pos ; i++) {
            this.array[i+1] = this.array[i];
        }
        this.array[pos] = data;
        this.usedSize++;
    }

    public boolean isFull() {
        if(this.usedSize == this.array.length) {
            return true;
        }
        return false;
    }


    private void resize() {
       this.array =  Arrays.copyOf(this.array,
               2*DEFAULT_SIZE);
    }
}
